package tools

import (
	models "fileshare/controller/modles"
	"fileshare/utils"
	"strconv"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

/**
* @program: fileshare
*
* @description:
*
* @author: Koco.Allien
*
* @create: 2020-04-05 18:46
**/

//获取返回的data数据和用户信息
func StartDate(c *gin.Context, title string, active int) (data map[string]interface{}, user models.UserInfo, ok bool) {
	data = make(map[string]interface{})
	data["title"] = title

	session := sessions.Default(c)
	if user, ok = session.Get("user").(models.UserInfo); !ok {
		//没有登陆
		data["username"] = user.Name
		data["role"] = 0
	} else {
		//登录
		data["uid"] = user.ID
		data["username"] = user.Name
		data["role"] = user.Role
	}

	data["active"] = active
	data["error"] = ""
	utils.MsgToLog("StartDate:用户信息和是否登录", user.Account, ok)
	return
}

//active  图标状态获取
/*
分享大厅 active = 1
个人文件 active = 2
个人分享 3
用户列表 4
个人信息 5
登录 登出0
*/
func GatQuery(c *gin.Context, key string, def int) (aid int) {
	active := c.Query(key)
	utils.LOGToLog("访问的页面的action和默认值", active, def)
	var err error
	aid, err = strconv.Atoi(active)
	if err != nil || aid < 0 {
		aid = def
	}
	return
}
